package com.ruoyi.community.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BbsArticleComment;
import com.ruoyi.community.mapper.BbsArticleCommentMapper;
import com.ruoyi.community.service.IBbsArticleCommentService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class IBbsArticleCommentServiceImpl implements IBbsArticleCommentService {
    @Autowired
    private BbsArticleCommentMapper bbsArticleCommentMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    private List<BbsArticleComment> tempReplys = new ArrayList<>();

    @Override
    public List<BbsArticleComment> listCommentByArticleId(Integer communityid,Long articleId) {
        List<BbsArticleComment> comments = bbsArticleCommentMapper.findByParentIdNull(communityid,articleId);
        for (BbsArticleComment comment : comments) {
            Long id = comment.getCommentid();
            String parentNickname = sysUserMapper.returnNameByUserid(comment.getCommenterid());
            comment.setNickname(parentNickname);
            List<BbsArticleComment> childComments = bbsArticleCommentMapper.findByParentIdNotNull(communityid,articleId,id);
            combineChildren(communityid,articleId,childComments);
            Collections.sort(tempReplys,(c1,c2)->{
                if(c1.getCreatetime().getTime()<c2.getCreatetime().getTime()) return -1;
                return 1;
            });
            comment.setReplyComments(tempReplys);
            tempReplys = new ArrayList<>();
        }
        Collections.sort(comments,(c1,c2)->{
            if(c1.getCreatetime().getTime()<c2.getCreatetime().getTime()) return -1;
            return 1;
        });
        return comments;
    }



    @Override
    public void combineChildren(Integer CommunityId, Long ArticleId, List<BbsArticleComment> childComments) {
        if(childComments.size()>0){
            for (BbsArticleComment childComment : childComments) {
                String commenterNickname = sysUserMapper.returnNameByUserid(childComment.getCommenterid());
                childComment.setNickname(commenterNickname);
                String commentedNickname = sysUserMapper.returnNameByUserid(childComment.getCommentedid());
                childComment.setParentNickname(commentedNickname);
                tempReplys.add(childComment);
                Long childId = childComment.getCommentid();
                recursively(CommunityId, ArticleId, childId);
            }
        }
    }

    @Override
    public void recursively(Integer CommunityId, Long ArticleId, Long childId) {
        List<BbsArticleComment> replyComments = bbsArticleCommentMapper.findByBlogIdAndReplayId(CommunityId, ArticleId,childId);
        if(replyComments.size() > 0){
            for (BbsArticleComment replyComment : replyComments) {
                String commenterNickname = sysUserMapper.returnNameByUserid(replyComment.getCommenterid());
                replyComment.setNickname(commenterNickname);
                String commentedNickname = sysUserMapper.returnNameByUserid(replyComment.getCommentedid());
                replyComment.setParentNickname(commentedNickname);
                Long replyId = replyComment.getCommentid();
                tempReplys.add(replyComment);
                recursively(CommunityId,ArticleId,replyId);
            }
        }
    }

    @Override
    public int insert(BbsArticleComment bbsArticleComment) {
        Integer communityid = SecurityUtils.getCommunityId();
        bbsArticleComment.setCommunityid(communityid);
        Long commenterid = SecurityUtils.getUserID();
        bbsArticleComment.setCommenterid(commenterid);
        return bbsArticleCommentMapper.insert(bbsArticleComment);
    }

    @Override
    public int delete(Long commentid) {
//        Integer communityid = SecurityUtils.getCommunityId();
        return bbsArticleCommentMapper.delete(commentid);
    }

    @Override
    public int update(BbsArticleComment bbsArticleComment) {
        return bbsArticleCommentMapper.updateByPrimaryKeySelective(bbsArticleComment);
    }

    @Override
    public List<BbsArticleComment> listComments(Integer communityid, Long articleId) {
        List<BbsArticleComment> list = bbsArticleCommentMapper.findParentComments(communityid, articleId);
        for(BbsArticleComment parentComment: list){
            int count = bbsArticleCommentMapper.countChildComments(parentComment.getCommentid());
            parentComment.setReplyCommentsCount(count);
            if(count > 0){
                parentComment.setReplyComments(bbsArticleCommentMapper.findReplyComments(parentComment.getCommentid()));
            }
        }
        return list;
    }

    @Override
    public List<BbsArticleComment> listAllComments(Long commentid) {
        List<BbsArticleComment> list = bbsArticleCommentMapper.findAllReplyComments(commentid);
        return list;
    }
}
